package com.yrline.pay.web.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.yrline.pay.proxy.PayInvoke;
import com.yrline.pay.proxy.response.AutoSignResponse;
import com.yrline.pay.proxy.response.CommonResponse;
import com.yrline.pay.proxy.response.FreezeResponse;
import com.yrline.pay.proxy.response.RechargeResponse;
import com.yrline.pay.proxy.response.UserQueryResponse;
import com.yrline.pay.proxy.response.UserResponse;
import com.yrline.pay.services.OperatorServices;
import com.yrline.pay.services.RechargeServices;
import com.yrline.pay.services.UserServices;

@Controller
public class PayCallbackController {

	@Autowired
	PayInvoke payInvoke;
	
	@Autowired
	UserServices userServices;
	
	@Autowired
	RechargeServices rechargeServices;
	
	@Autowired
	OperatorServices operatorServices;
	
	private Logger logger = org.slf4j.LoggerFactory
			.getLogger(PayCallbackController.class);
	
	@RequestMapping(value = "/callback/{action}")
	public String callback(@Valid  CommonResponse commonResponse,@PathVariable("action") Integer action,Model model) {
		logger.info(commonResponse.toString());
		if(action==1){
			UserResponse response=	(UserResponse)payInvoke.unSign(commonResponse, UserResponse.class);
			userServices.completeUser(response);
		} else if(action==3){
			RechargeResponse response=	(RechargeResponse)payInvoke.unSign(commonResponse, RechargeResponse.class);
			rechargeServices.completeRecored(response);
		} else if (action==4){
			FreezeResponse response=	(FreezeResponse)payInvoke.unSign(commonResponse, FreezeResponse.class);
			operatorServices.completeOperator(response);
		}else if (action==6){
			AutoSignResponse response=	(AutoSignResponse)payInvoke.unSign(commonResponse, AutoSignResponse.class);
			userServices.autoSignCallback(response);
		}
		model.addAttribute("info", commonResponse);
		return "success";
	}
}
